#!/bin/bash

function conv {
	echo "jestem w \"$1\""
	dirs=`ls -ld -C1 */`
	for dir in ${dirs} 
	do	
		cd "${dir}"
		conv "${dir}"
		cd ..
	done

	files=`ls -C1 *.${extIn}`
	for file in $files
	do
		fileName=${file%.${extIn}}
		if [ -f ${fileName}.txt ]; then
			echo "===================================================================="
			echo "Znaleziono plik z napisami dla pliku ${fileName}.${extIn}"
			echo "===================================================================="
			mencoder "${fileName}.${extIn}" -sub "${fileName}.txt" -o "${fileName}.${extOut}" -oac copy -ovc lavc -lavcopts vbitrate=1000 -subcp cp1250 -font arial -subfont-text-scale '4.0' -subfont-blur '1' -subfont-outline '1.5' -ffactor '3' -autosub -subwidth '90'
#			ffmpeg -i ${fileName}.${extIn} -ab 192k ${fileName}.${extOut}
#			if [ -f ${fileName}.${extOut} ]; then
#				rm ${fileName}.${extIn}
#				echo "Source file ${fileName}.${extIn} has been removed"
#			fi
		else
			echo "===================================================================="
			echo "Nie znaleziono pliku z napisami dla pliku ${fileName}.${extIn}"
			echo "===================================================================="
		fi
	done
}

IFS=$'\n'

extIn="avi"
extOut="hs.avi"
echo "===================================================================="
echo "FINDING ${extIn} FILES "
echo "===================================================================="
conv "`pwd`"

extIn="mkv"
#extOut="hs.${extIn}"
echo "===================================================================="
echo "FINDING ${extIn} FILES "
echo "===================================================================="
conv "`pwd`"

extIn="mp4"
#extOut="hs.${extIn}"
echo "===================================================================="
echo "FINDING ${extIn} FILES "
echo "===================================================================="
conv "`pwd`"

echo "DONE !"

exit

mencoder "$inFile" -sub "$textFile" -o "$outFile" -oac copy -ovc lavc -lavcopts vbitrate=1000 -subcp cp1250 -font arial -subfont-text-scale '4.0' -subfont-blur '1' -subfont-outline '1.5' -ffactor '3' -autosub -subwidth '90'
